package cn.fufeiii.bt.easyexcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.support.ExcelTypeEnum;
import lombok.Data;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Consumer;

/**
 * @author FuFei
 */
public class ExcelMain {


    public static void main(String[] args) {
        Map<String, String> map = new LinkedHashMap<>();
        Consumer<ExcelHead> dataAccepter = it -> {
            String text = it.getText() == null ? "" : it.getText();
            map.compute(it.getCode(), (key, oldValue) -> oldValue == null ? text : text + " OR " + oldValue);
        };
        Consumer<AnalysisContext> analysisContextAccepter = it -> {
            // 这个行数是按照最后一行被使用的单元格所在行数计算的
            // 没有忽略中间的空行和标题行
            System.out.println("当前Sheet的占用行数 = " + it.readSheetHolder().getApproximateTotalRowNumber());
        };
        SingleReadListener<ExcelHead> singleReadListener = new SingleReadListener<>(dataAccepter, analysisContextAccepter);

        EasyExcel.read("D:\\IdeaProject\\OpenSource\\backend-template\\app-solution-boot\\src\\main\\resources\\checkout-code-mapping.xlsx")
                .excelType(ExcelTypeEnum.XLSX)
                .head(ExcelHead.class)
                .registerReadListener(singleReadListener)
                .doReadAll();

        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.printf("M%s(\"%s\", \"%s\"),%n", entry.getKey(), entry.getKey(), entry.getValue());
        }
        System.out.println("数据总行数 = " + map.entrySet().size());
    }

    @Data
    public static class ExcelHead {
        @ExcelProperty(value = "Code")
        private String code;

        @ExcelProperty(value = "Text")
        private String text;

        @ExcelProperty(value = "Additional Information")
        private String extra;
    }

}
